package com.clickpaas.governmentcloud.service.policymatchextservice.impl;

import com.bizcloud.ipaas.tb4093760b8224c4fbcff37d3fa266753.d20210127152100.model.*;
import com.clickpaas.governmentcloud.common.ConstantExt;
import com.clickpaas.governmentcloud.common.ConstantSuggest;
import com.clickpaas.governmentcloud.common.EnumCompanyType;
import com.clickpaas.governmentcloud.common.ServerResponse;
import com.clickpaas.governmentcloud.entity.policyEntity.PolicyEntity;
import com.clickpaas.governmentcloud.service.policymatchextservice.MatchExtService;
import com.clickpaas.governmentcloud.utils.policycondcalutils.PolicyCondCal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class SpecialSMECompUpgradeServiceImpl implements MatchExtService {
    @Resource
    private PolicyCondCal policyCondCal;
    /**
     * 企业政策常量：中小企业发展专项资金（中小企业升级）
     */
    private final static String _SPECIAL_FSME_UPGRADE = ConstantExt._SPECIAL_FSME_UPGRADE;

    /**
     * 跟政策是否匹配
     *
     * @param policy
     * @return
     */
    @Override
    public ServerResponse<?> isMatchSuccess(PolicyEntity policy) {
        /**
         * 建议
         */
        StringBuffer suggest = new StringBuffer();

        /**
         * 返回信息
         */
        ServerResponse<?> serverResponse = ServerResponse.createError(ConstantExt._ERROR_MESSAGE);

        /**
         * 评分
         */
        Integer score = ConstantExt._ZERO;
        /**
         * 是否快速匹配：Y->true,N->false
         */
        boolean isQuickMatch = ConstantExt.QUICK_MATCH.equals(policy.getType());
        /**
         *  必要条件判断
         */
        HashMap hashMap = necessCondi(policy);
        /**
         * 必要条件对快速匹配数据做处理（精确匹配、快速匹配）
         */
        boolean ifNecess=Boolean.parseBoolean(hashMap.get(ConstantExt._NECESS).toString());
        if(!ifNecess && isQuickMatch){
            return serverResponse;
        }
        /**
         * 累加必要条件评分
         */
        score = score + Integer.parseInt(hashMap.get(ConstantExt._SCORE).toString());
        suggest.append(hashMap.get(ConstantExt._SUGGEST).toString());

        HashMap suggAndScore = getScore(policy);
        /**
         * 累加评分
         */
        score = score + Integer.parseInt(suggAndScore.get(ConstantExt._SCORE).toString());
        /**
         * 评分为0，快速匹配不展示数据
         */
        if(score <= 0 && isQuickMatch){
            return serverResponse;
        }
        /**
         * 添加建议
         */
        suggest.append(suggAndScore.get(ConstantExt._SUGGEST).toString());
        /*if (suggest.length() == 0 && score >= 90) {
            suggest.append("建议申报");
        }*/

        /**
         * 快速匹配不添加建议
         */
        if (isQuickMatch){
            suggest = new StringBuffer();
        }
        /**
         * 必要条件不满足得0分
         */
        if(!ifNecess){
            score = 0;
        }
        return insertPolicyData(policy,score,suggest,serverResponse);
    }

    /**
     * 计算企业评分
     *
     * @param policy
     * @return
     */
    @Override
    public HashMap getScore(PolicyEntity policy) {
        HashMap<String, Object> returnValue = new HashMap<>();
        StringBuffer suggest = new StringBuffer();
        boolean flagSug = false;
        /**
         * 分值
         */
        Integer score = ConstantExt._ZERO;
        /**
         * 是否中小企业
         */
        if(policyCondCal.ifMediumOrMiniComp(policy)){
            score += 10;
        }else {
            flagSug = true;
        }
        /**
         * 上年度投资情况:
         */
        /**
         * 上一年度通过融资租赁方式购置先进适用设备
         */
        if(null != policy.getFinanceLeasing()
                && ConstantExt._YES.equals(policy.getFinanceLeasing())){
            score += 10;
        }else {
            flagSug = true;
        }
        /**
         * 购置智能制造核心设备，包括但不限于：数控机床、工业机器人、增材制造设备、智能传感与控制设备、智能检测与装配设备、智能物流与仓储装备
         * 设备用于：数控机床、工业机器人、增材制造设备、智能传感与控制设备、智能检测与装配设备、智能物流与仓储装备、其他
         */
        if(null != policy.getEquipment()
                && equitmentList().contains(policy.getEquipment())){
            score += 40;
        }else {
            flagSug = true;
        }
        /**
         * 单个设备金额或多个设备累计额不低于50万元
         * 设备采购金额是否大于50万；大于得38分、小于得8分
         */
        if (null == policy.getMorethanFiveHundredThousand()){
            //空值不给分
            score += 0;
            flagSug = true;
        }else if (ConstantExt._YES.equals(policy.getMorethanFiveHundredThousand())){
            score += 38;
        }else {
            score += 8;
            flagSug = true;
        }
        if (flagSug){
            suggest.append(ConstantSuggest._UPGRADE_INVESTMENT_SITUATION);
        }
        returnValue.put(ConstantExt._SCORE,score);
        returnValue.put(ConstantExt._SUGGEST,suggest);
        return returnValue;
    }
    /**
     * 写入匹配结果
     *
     * @param policy
     * @return
     */
    @Override
    public ServerResponse insertPolicyData(PolicyEntity policy,Integer score,StringBuffer suggest,ServerResponse serverResponse) {
        /**
         * 查询当前政策信息
         */
        TPoliciesDTO policyInfo = policyCondCal.getPolicyInfo(_SPECIAL_FSME_UPGRADE);

        if (null == policyInfo) {
            log.info("政策：【{}】查询不到",_SPECIAL_FSME_UPGRADE);
            return serverResponse;
        }

        policyCondCal.insertCommonPolicyData(policy, policyInfo, score, suggest);
        return ServerResponse.createSuccess(ConstantExt._SUCCESS_MESSAGE);
    }

    /**
     * 默认设备列表
     * @return
     */
    public List<String> equitmentList(){
        ArrayList<String> equitList = new ArrayList<>();
        equitList.add(EnumCompanyType._CNCMACHINETOOLS.getCompanyTypeChina());
        equitList.add(EnumCompanyType._INDUSTRIALROBOT.getCompanyTypeChina());
        equitList.add(EnumCompanyType._ADDMANUFACTURINGEQU.getCompanyTypeChina());
        equitList.add(EnumCompanyType._INTELLSENSINGCONTROLEQU.getCompanyTypeChina());
        equitList.add(EnumCompanyType._INTELLTESTASSEMBLYEQU.getCompanyTypeChina());
        equitList.add(EnumCompanyType._INTELLILOGISTICSWAREQU.getCompanyTypeChina());
        return equitList;
    }

    /**
     * 必要条件判断
     *
     * @param policy
     * @return
     */
    @Override
    public HashMap necessCondi(PolicyEntity policy) {
        HashMap<String, Object> map = new HashMap<>();
        Integer score = ConstantExt._ZERO;
        map.put(ConstantExt._NECESS,true);
        StringBuffer suggStr = new StringBuffer();
        map.put(ConstantExt._SCORE,score);
        map.put(ConstantExt._SUGGEST,suggStr);
        return map;
    }
}
